package com.ijecorp.media.audio;

import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;

public abstract class AudioReader {
	protected DataInputStream in;
	protected File audioFile;
	protected AudioFormat audioFormat;

	protected AudioReader(File f){
		this.audioFile = f;
	}

	public final static AudioReader getAudioReader(File f, int type){

		switch(type){
		//: Free and open file formats
		case AudioFormat.WAVE:
			return new WaveReader(f);			
		case AudioFormat.OGG:
			break;
		case AudioFormat.FLAC:
			break;
		case AudioFormat.AIFF:
			break;
		case AudioFormat.RAW:
			break;
		case AudioFormat.AU: 
			break;
			//: Open file formats
		case AudioFormat.GSM: 
			break;
		case AudioFormat.DCT: break;
		case AudioFormat.VOX: 
			break;
		case AudioFormat.AAC: 
			break;	
		case AudioFormat.MPEG4:
			break;
			//: Proprietary formats
		case AudioFormat.MP3: 
			break;
		case AudioFormat.WMA: 
			break;
		case AudioFormat.ATRAC: 
			break;
		case AudioFormat.RA: 
			break;
		case AudioFormat.RAM: 
			break;
		case AudioFormat.DSS: 
			break;
		case AudioFormat.MSV: 
			break;
		case AudioFormat.DVF: 
			break;
		case AudioFormat.M4P: 
			break; 
		}
		throw new RuntimeException("Not support audio format "  + type);
	}

	public final boolean open(){
		try {
			in = new DataInputStream( new FileInputStream(audioFile));			
			this.checkFormat();
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}
	
	public final boolean close(){
		if(this.in == null ){
			throw new RuntimeException("Not open " + this.audioFile);
		}		
		try {
			this.in.close();
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		}
		this.in = null;
		return true;
	}
	

	public abstract void checkFormat();
	public abstract void read();
	public final AudioFormat getAudioFormat(){
		return this.audioFormat;
	}
}
